package client;
//Eddie Zhou and Peter Chen, Per. F  
//Owning.

import javax.swing.JComponent;
import javax.swing.JFrame;

import core.Location;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.*;

public class GridBuild extends JFrame{
	
	private int height, width;
	public static final int HEIGHT = 800, WIDTH = 1000;
	private final int HEX_LENGTH = 60;
	private final int MID_PT_SIZE = 5;
	
	public GridBuild(int h, int w)
	{
		height = h;
		width = w;
		init();
	}
	
	
	//Conversions
	public Location getHexCoord(int aX, int aY)
	{
		int y = (int)((aY - (HEIGHT / 2)) / ((HEX_LENGTH / 2) * Math.pow(3, 0.5)));
		int x = (int)(((aX - (WIDTH / 2)) - (HEX_LENGTH / 2) * y) / (HEX_LENGTH));
		
		Location l = new Location(x, y);
		return l;
	}

	public int getNormY(Location a)
	{
		int yp = a.getY();
		return (int) ((30 * Math.pow(3, 0.5) * yp) + (HEIGHT / 2));
	}
	
	public int getNormX(Location a)
	{
		int xp = a.getX();
		return (60 * xp) + (30 * a.getY()) + (WIDTH / 2);
	}
	
	//Drawing
	
	public void init() {
		setSize(WIDTH,HEIGHT);
		repaint();
	}
	
	public void paint(Graphics g) {
		
		Graphics2D g2  = (Graphics2D) g;
		g2.setColor(Color.WHITE);
		g2.fillRect(0, 0, WIDTH, HEIGHT);
		g2.setColor(Color.RED);
		g2.fillRect((WIDTH / 2) - (MID_PT_SIZE / 2), 
				(HEIGHT / 2) - (MID_PT_SIZE / 2), MID_PT_SIZE, MID_PT_SIZE);
//		Location l = new Location(0, 0);
//		g2.fillRect(getNormX(l) - (MID_PT_SIZE / 2), 
//				getNormY(l) - (MID_PT_SIZE / 2), MID_PT_SIZE, MID_PT_SIZE);
		
		Location l = new Location(0, 0);
		drawHex(g2, l);
		
		for(int i = 1; i <= 2; i++)
		{
			Location a = new Location(l.getX() + 1 * i, l.getY() - 2 * i);
			drawHex(g2, a);
			Location b = new Location(l.getX() + 2 * i, l.getY() - 1 * i);
			drawHex(g2, b);
			Location c = new Location(l.getX() + 1 * i, l.getY() + 1 * i);
			drawHex(g2, c);
			Location d = new Location(l.getX() - 1 * i, l.getY() + 2 * i);
			drawHex(g2, d);
			Location e = new Location(l.getX() - 2 * i, l.getY() + 1 * i);
			drawHex(g2, e);
			Location f = new Location(l.getX() - 1 * i, l.getY() - 1 * i);
			drawHex(g2, f);
		}
		
		Location h = new Location(l.getX(), l.getY() - 4);
		g2.drawRect(getNormX(h) - (MID_PT_SIZE / 2),
				getNormY(h) - (MID_PT_SIZE / 2),
				MID_PT_SIZE, MID_PT_SIZE);
		Location i = new Location(l.getX() + 4, l.getY() - 4);
		g2.drawRect(getNormX(i) - (MID_PT_SIZE / 2),
				getNormY(i) - (MID_PT_SIZE / 2),
				MID_PT_SIZE, MID_PT_SIZE);
		Location j = new Location(l.getX() + 4, l.getY());
		g2.drawRect(getNormX(j) - (MID_PT_SIZE / 2),
				getNormY(j) - (MID_PT_SIZE / 2),
				MID_PT_SIZE, MID_PT_SIZE);
		Location k = new Location(l.getX(), l.getY() + 4);
		g2.drawRect(getNormX(k) - (MID_PT_SIZE / 2),
				getNormY(k) - (MID_PT_SIZE / 2),
				MID_PT_SIZE, MID_PT_SIZE);
		Location m = new Location(l.getX() - 4, l.getY() + 4);
		g2.drawRect(getNormX(m) - (MID_PT_SIZE / 2),
				getNormY(m) - (MID_PT_SIZE / 2),
				MID_PT_SIZE, MID_PT_SIZE);
		Location n = new Location(l.getX() - 4, l.getY());
		g2.drawRect(getNormX(n) - (MID_PT_SIZE / 2),
				getNormY(n) - (MID_PT_SIZE / 2),
				MID_PT_SIZE, MID_PT_SIZE);
		
	}
	
	public void drawHex(Graphics2D g2, Location center)
	{
		for(int i = -1 + center.getX(); i <= 1 + center.getX(); i++)
			for(int j = -1 + center.getY(); j <= 1 + center.getY(); j++)
			{
				if((i - j) % 3 != 0)
				{
					g2.setColor(Color.BLACK);
					Location l1 = new Location(i, j);
					g2.drawRect(getNormX(l1) - (MID_PT_SIZE / 2), 
							getNormY(l1) - (MID_PT_SIZE / 2), 
							MID_PT_SIZE, MID_PT_SIZE);
				}
			}
	}
	

}
